package com.iuie.ns.datum.service.impl;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.iuie.ns.datum.dao.IBlogDao;
import com.iuie.ns.datum.mapper.BlogMapper;
import com.iuie.ns.datum.service.IBlogService;
import com.iuie.ns.datum.service.IFileInfoService;
import com.iuie.ns.datum.service.IFileService;
import com.iuie.ns.datum.vo.BlogInfoVO;
import com.iuie.ns.datum.vo.FileInfoVO;
import com.iuie.ns.frame.exception.ServiceException;
import com.iuie.ns.frame.persistence.dao.IGenericDao;
import com.iuie.ns.frame.service.impl.BaseServiceImpl;
import com.iuie.ns.frame.utils.ShiroUtils;
import com.iuie.ns.frame.utils.StringUtils;
import com.iuie.ns.frame.utils.URLUtil;
import com.iuie.ns.system.vo.user.SysUserVO;

@Service("blogService")
public class BlogServiceImpl extends BaseServiceImpl<BlogInfoVO> implements IBlogService {
	
	@Resource
	private IBlogDao blogDao;

	@Resource
	private IFileInfoService fileInfoService;
	
	@Resource
	private IFileService fileService;
	
	@Resource
	private BlogMapper blogMapper;
	
	@Override
	protected IGenericDao<BlogInfoVO, Serializable> getGenericDao() {
		return blogDao;
	}

	@Override
	public List<BlogInfoVO> findByBlogType(String blogType) {
		return blogMapper.findByBlogType(blogType);
	}
	
	@Override
	public BlogInfoVO addBlog(BlogInfoVO blogInfo) throws IOException {
		// fileId,blogType,author不能为空
		if (blogInfo.getBlogName()==null ) {
			throw new NullPointerException("博客名称不能为空");
		}
		if (blogInfo.getFileInfo() == null || blogInfo.getFileInfo().getFileId()==null) {
			if (blogInfo.getLink() == null) {
				throw new NullPointerException("必须上传博客文件或设置博客的超链接");
			} else {
				// 验证本博客超链接是否合法
				if(!URLUtil.isValid(blogInfo.getLink()))
					throw new RuntimeException("不合法的超链接：" + blogInfo.getLink());
			}
			FileInfoVO fileInfo = new FileInfoVO();
			fileInfo.setFileId(System.currentTimeMillis());
			fileInfo.setFileName(blogInfo.getLink());
			fileInfo.setOriginalName(blogInfo.getBlogName());
			fileInfo.setUploadTime(new Date());
			fileInfo.setFileType("blog");
			
			fileInfoService.addFileInfo(fileInfo, true);
			blogInfo.setFileInfo(fileInfo);
		}
		
		if (blogInfo.getBlogType()==null || blogInfo.getBlogType().getCodeInfoValue()==null ) {
			throw new NullPointerException("博客类型不能为空");
		}
		SysUserVO user = (SysUserVO) ShiroUtils.getCurrentUser();
//		SysUserVO user = new SysUserVO();
//		user.setUserId("10000001");
//		user.setUserName("刘杰");
		if (user ==null ) {
		    throw new ServiceException("当前登录用户异常，保存博客失败！");
		}
		blogInfo.setAuthor(user.getUserName());
		blogInfo.setAuthorId(user.getUserId());
		
		// 更新createTime为最新时间
		blogInfo.setCreateTime(new Date());
		BlogInfoVO uBlogInfo = null;
		if (blogInfo.getBlogId() == null) {
			// 新增博客
			blogInfo.setBlogId(StringUtils.timestampId());
			blogDao.addBlog(blogInfo);
			// 在上传文件的情况下，blogInfo.fileInfo.fileName = null
			// 而如果未上传文件，blogInfo.fileInfo.fileName = blogInfo.link
			if (blogInfo.getFileInfo().getFileName() == null) {
				fileService.letFileNameLegal(blogInfo.getFileInfo().getFileId());
			}
		} else {
			// 修改博客
			blogDao.updateBlog(blogInfo);
			uBlogInfo = blogInfo;
		}
		return uBlogInfo;
	}

	@Override
	public void updateBySignRead(Long blogId) {
		BlogInfoVO blogInfo = blogMapper.findById(blogId);
		if (blogInfo == null) {
            throw new ServiceException("该博客已经被删除！");
        }
		Integer visits = blogInfo.getVisits() + 1;
		blogInfo.setVisits(visits);
	}

	@Override
	public void deleteById(Long id) {
		// 1. 查询blogInfo
		// 2. 取出fileInfo
		BlogInfoVO blogInfo = blogMapper.findById((Long) id);
		FileInfoVO fileInfo = blogInfo.getFileInfo();
		// 3. 删除blogInfo
		// 4. 删除file和fileInfo
		blogMapper.deleteById(id);
		if (fileInfo!=null) {
			fileService.deleteFileById(fileInfo.getFileId(), true, "blog");
		}
	}

	@Override
	public BlogInfoVO findById(Long id) {
		BlogInfoVO blogInfo = blogMapper.findById(id);
		if (blogInfo == null) {
			throw new ServiceException("博客已被删除！");
		}
		return blogInfo;
	}

}
